﻿@model List<ActivityLogTypeModel>
@{  
    //page title
    ViewBag.Title = T("Admin.Configuration.ActivityLog.ActivityLogType").Text;
}
@using (Html.BeginForm("SaveTypes", "ActivityLog"))
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-configuration.png")" alt="" />
            @T("Admin.Configuration.ActivityLog.ActivityLogType")
        </div>
        <div class="options">
            <input type="submit" name="save" class="k-button" value="@T("Admin.Common.Save")" />
        </div>
    </div>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <div id="activityLogType-grid"></div>

                <script>

                    //local datasource
                    var activityLogTypes = [
                        @for(int i=0;i<Model.Count;i++)
                    {
                        var alt = Model[i];
                        <text>
                        {
                            Id: @(alt.Id),
                            Name: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(alt.Name)))",
                            Enabled: @(alt.Enabled.ToString().ToLowerInvariant())
                        }
                    </text>
                        if (i != Model.Count - 1)
                        {
                            <text>,</text>
                        }
                    }
                    ];

                    $(document).ready(function () {
                        $("#activityLogType-grid").kendoGrid({
                            dataSource: {
                                data: activityLogTypes,
                                schema: {
                                    model: {
                                        fields: {
                                            Id: { type: "number" },
                                            Name: { type: "string" },
                                            Enabled: { type: "boolean" }
                                        }
                                    }
                                },
                                error: function (e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                }
                            },
                            pageable: {
                                refresh: true,
                                numeric: false,
                                previousNext: false,
                                info: false
                            },
                            scrollable: false,
                            columns: [
                                {
                                    field: "Name",
                                    title: "@T("Admin.Configuration.ActivityLog.ActivityLogType.Fields.Name")",
                                    width: 100
                                }, {
                                    field: "Enabled",
                                    headerTemplate: "@T("Admin.Configuration.ActivityLog.ActivityLogType.Fields.Enabled")&nbsp<input id='mastercheckbox' type='checkbox' />",
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: "<input name='checkbox_activity_types' value='#=Id#' type='checkbox' class='checkboxGroups' # if(Enabled) {# checked='checked' #}  # />",
                                    width: 100
                                }
                            ]
                        });
                    });

                </script>
                

                <script type="text/javascript">

                    $(document).ready(function () {

                        updateMasterCheckbox();

                        $('#mastercheckbox').click(function () {
                            $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                        });

                        //wire up checkboxes.
                        $('#activityLogType-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                            updateMasterCheckbox();
                        });
                    });

                    function updateMasterCheckbox() {
                        var numChkBoxes = $('#activityLogType-grid input[type=checkbox][id!=mastercheckbox]').length;
                        var numChkBoxesChecked = $('#activityLogType-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
                        $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                    }
                </script>   
            </td>
        </tr>
    </table> 
}